<?php

declare(strict_types=1);

namespace App\CustHub\Model;

use Mine\MineModel;

/**
 * @property int $id 聊天列表ID
 * @property int $main_uid 主账号ID
 * @property int $user_id 客服ID
 * @property int $talk_type 聊天类型[1:私信;2:群聊;]
 * @property int $robot_id 机器人ID
 * @property string $receiver_id 接收者ID（用户ID 或 群ID）
 * @property int $is_top 是否置顶[0:否;1:是]
 * @property int $is_robot 是否机器人[0:否;1:是;]
 * @property int $is_delete 是否删除[0:否;1:是;]
 * @property int $is_disturb 消息免打扰[0:否;1:是;]
 * @property int $is_hidden 是否屏蔽：0=否 1=是
 * @property int $is_pending 是否待办：0=否 1=是
 * @property int $is_blocked 是否拉黑：0=否 1=是
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 更新时间
 * @property int $cs_num 客服发送数量
 * @property int $contact_num 客户发送数量
 * @property int contact_first_reply 客户第一次回复时间
 * @property int $unread_num 未读消息数量
 * @property-read null|ChContact $contact
 * @property-read null|ChCustomerService $customerService 
 * @property-read null|ChRobot $robot 
 */
class ChTalkSession extends MineModel
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'ch_talk_session';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'main_uid', 'user_id', 'talk_type', 'robot_id', 'receiver_id', 'is_top', 'is_robot', 'is_delete', 'is_disturb', 'is_hidden', 'is_pending', 'is_blocked', 'created_at', 'updated_at', 'cs_num', 'contact_num', 'contact_first_reply'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'main_uid' => 'integer', 'user_id' => 'integer', 'talk_type' => 'integer', 'robot_id' => 'integer', 'is_top' => 'integer', 'is_robot' => 'integer', 'is_delete' => 'integer', 'is_disturb' => 'integer', 'is_hidden' => 'integer', 'is_pending' => 'integer', 'is_blocked' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'cs_num' => 'integer', 'contact_num' => 'integer', 'unread_num' => 'integer'];

    /**
     * @param array $params
     * @return array
     */
    public static function item(array $params): array
    {
        $item = [
            'id'          => 0,
            'talk_type'   => 0,
            'robot_id'    => 0,
            'receiver_id' => 0,
            'robot_nickname' => '',
            'robot_avatar' =>'',
            'robot_corp_name' => '',
            'is_top'      => 0,
            'is_disturb'  => 0,
            'is_online'   => 0,
            'is_pending'  => 0,
            'is_hidden'   => 0,
            'is_robot'    => 0,
            'is_blocked'  => 0,
            'avatar'      => '',
            'name'        => '',
            'remark_name' => '',
            'unread_num'  => 0,
            'msg_text'    => '',
            'updated_at'  => date('Y-m-d H:i:s')
        ];

        return array_merge($item, array_intersect_key($params, $item));
    }

    public function contact()
    {
        return $this->hasOne(ChContact::class, 'user_id', 'receiver_id')
            ->where('ch_contact.robot_id',$this->robot_id);
    }

    public function customerService(): \Hyperf\Database\Model\Relations\HasOne
    {
        return $this->hasOne(ChCustomerService::class, 'id', 'user_id');
    }

    public function robot(): \Hyperf\Database\Model\Relations\HasOne
    {
        return $this->hasOne(ChRobot::class, 'id', 'robot_id');
    }
}
